*Dofile TESTS


use "$Endline_dtRaw/tests/data_tests1.dta", clear

append using "$Endline_dtRaw/tests/data_tests2.dta"

duplicates drop

sort school_id teste classe turma turno
quietly by school_id teste classe turma turno:  gen dup = cond(_N==1,0,_n)

drop if school_id==12 & teste==1 & classe==3 & turma==1 & turno==2 & teacher_id==99
drop if school_id==12 & teste==2 & classe==3 & turma==1 & turno==2 & teacher_id==99


drop if school_id==97 & teste==1 & classe==4 & turma==2 & turno==1 & teacher_id==99
drop if school_id==105 & teste==2 & classe==5 & turma==1 & turno==1 & teacher_id==3 & aluno_028=="  "

drop if school_id==112 & teste==1 & classe==3 & turma==3 & turno==1 & teacher_id==9 & aluno_001==" 0"
drop if school_id==112 & teste==2 & classe==3 & turma==3 & turno==1 & teacher_id==9 & aluno_001==" 9"

drop if school_id==118 & teste==1 & classe==4 & turma==1 & turno==1 & teacher_id==3 & aluno_001=="14"
drop if school_id==118 & teste==2 & classe==4 & turma==1 & turno==1 & teacher_id==3 & aluno_001==" 0"

drop if school_id==124 & teste==1 & classe==4 & turma==2 & turno==1 & teacher_id==99
drop if school_id==124 & teste==1 & classe==4 & turma==3 & turno==1 & teacher_id==3 & aluno_006=="99"

drop if school_id==124 & teste==2 & classe==4 & turma==2 & turno==1 & teacher_id==99
drop if school_id==124 & teste==2 & classe==4 & turma==3 & turno==1 & teacher_id==3 & aluno_006=="99"

drop if school_id==21 & teste==2 & classe==4 & turma==4 & turno==1 & teacher_id==11 & obs_id==35

drop if school_id==22 & teste==1 & classe==5 & turma==1 & turno==1 & teacher_id==7 & aluno_001==" 0"

drop if school_id==71 & teste==1 & classe==4 & turma==5 & turno==2 & teacher_id==10 & aluno_001=="22"

replace turma=1 if school_id==119 & classe==4 & turma==3 & turno==2 & teacher_id==11

drop dup

replace school_id=999 if teacher_id==99

**

drop fim

rename teste test_type
rename classe grade
rename turma class
rename turno shift
rename obs_id enumerator_id

lab var school_id     "ID school"
lab var test_type     "Mathematics or Portuguese test"
lab var grade         "Grade of student"
lab var class   			"Class/group of student"
lab var shift   			"Shift of student's class"
lab var teacher_id   	"ID teacher of student"
lab var enumerator_id "ID enumerator observing the test"

gen n = _n

forval x=1/9 {
rename *_00`x' *_`x'
}

forval x=10/99 {
rename *_0`x' *_`x'
}

reshape long aluno_ q1_ q2_ q3_ q4_ q5_ q6_ q7_ q8_ q9_ q10_, i(n) j(student_id)

rename *_ *

drop n

rename aluno parent_id

lab var parent_id  	"ID parent of student"
lab var student_id	"ID student per class"

destring parent_id q1 q2 q3 q4 q5 q6 q7 q8 q9 q10, replace

replace parent_id = . if inlist(parent_id,0,88,99)

foreach var in q1 q2 q3 q4 q5 q6 q7 q8 q9 q10{
replace `var' = . if inlist(`var',8,9)
replace `var' = 0 if inlist(`var',5)
}

drop if q1==. & q2==. & q3==. & q4==. & q5==. & q6==. & q7==. & q8==. & q9==. & q10==.

lab def test_type 1 "Portuguese" 2 "Mathematics"
lab val test_type test_type

lab def grade 3 "3rd grade" 4 "4th grade" 5 "5th grade"
lab val grade grade

#delimit ;
;
label define class
     0 "No letter"
     1 "A"
     2 "B"
     3 "C"
     4 "D"
     5 "E"
     6 "F"
     7 "G"
     8 "H"
     9 "I"
    10 "J"
    11 "K"
    12 "L"
    13 "M"
    14 "N"
    15 "O"
    16 "P"
    17 "Q"
    18 "R"
    19 "S"
    20 "T"
	21 "A1"
	22 "A2"
	23 "C1"
	24 "C2"
	25 "J1"
	26 "J2"
	27 "J3"
	28 "J4"
	29 "J5"
	30 "J6"
	31 "J7"
	32 "J8"
	33 "J9"
;
label define enum
1	"Antonio Lopes Miguel Rocha"
2	"Augusta Júlio Miranda"
3	"César Zau Joaquim Morais Mendes"
4	"Edesdeth Abrantes Fonseca"
5	"Fernando Portugal Leonardo"
6	"Filipe Quissana Cariengue"
7	"Herlander Mauro Lemos Lima"
8	"Manuel Aguinaldo Barroso Moreira Francisco"
9	"Albertina de Oliveira Armando"
10	"Augusto Viana Kupinga"
11	"Beatriz Laurinda Teixeira da Costa"
12	"Carlos da Fonseca Ricardo "
13	"Carolina Germano da Silva"
14	"Felizarda Luis Avelino"
15	"Hermínia Caetano Kalolo"
16	"Ilídio Duarte Reis Cardoso"
17	"Isabel Marcolino Lisboa"
18	"João Monteiro Bimbe"
19	"José Alberto Nungulo Kamucunja"
20	"Manuel Pinheiro Inacio Ernesto"
21	"Marimilia Francisco Fernando"
22	"José Bambi Kandjeke"
23	"Samora Paulo José Ganga"
24	"Victória Maneco Tavares"
25	"Adelaide Fernando Miguens"
26	"Adelaide Marcelina da Cruz Zeferino"
27	"Adilson Fragoso Pascoal Inácio Filipe"
28	"Amaro Alberto Chipa"
29	"Ana Paula Silvestre Ventura"
30	"Antonio Calei Pedro da Silva"
31	"António Manuel da Silva"
32	"Bernardo Miguêns Gimbo"
33	"Bernardo Sungo Manuel Zau"
34	"Bráulio Eanes Gabriel Alberto"
35	"Bruno José Fernando Viegas"
36	"Carvalinho António Correia"
37	"Cirineu Jr. Jorge"
38	"Domingos Guimarães Sumano"
39	"Elvino António Sobral Salomão"
40	"Etelvina Albertina Francisco Teixeira"
41	"Helder Silvestre Lino Tomé Pinto"
42	"Joana Pinto Soma"
43	"Jonas Frederico Albino Rangel"
44	"José Catraio António"
45	"José Luciano Januário"
46	"José Maria Sumano Delgado"
47	"José Pinto Fançony"
48	"Magalhães Salissinga Gomes"
49	"Maria de Fátima Paulo Feliciano"
50	"Mariza Fernando Manuel Vales"
51	"Monteiro Bastos"
52	"Nilton Januário Valentim"
53	"Sara Claúdio Vidro Aguiar Maneco"
54	"Telma da Conceição Mweleyavo António"
55	"Teresa Aguiar da Costa"
56	"Teresa Kizua Kassucada"
57	"Bruno Avelino Segunda"
58	"Inocêncio Mário Cupenala"
59	"Sabino Jorge Bira"
;

#delimit cr

lab val class class
lab val enumerator_id enum

lab def shift 1 "Morning" 2 "Afternoon"
lab val shift shift

replace enumerator_id=. if enumerator_id==99

lab def Q 0 "empty" 1 "a" 2 "b" 3 "c" 4 "d"
lab val q1 q2 q3 q4 q5 q6 q7 q8 q9 q10 Q

forval x=1/10 {
lab var q`x' "student answer Q`x'"
}

forval x=1/10 {
gen q`x'_score=0
lab var q`x'_score "student score Q`x'"
}

*POR3

replace q1_score=1 	if q1==1 & 	grade==3 & test_type==1
replace q2_score=1 	if q2==4 & 	grade==3 & test_type==1
replace q3_score=1 	if q3==1 & 	grade==3 & test_type==1
replace q4_score=1 	if q4==3 & 	grade==3 & test_type==1
replace q5_score=1 	if q5==2 & 	grade==3 & test_type==1
replace q6_score=1 	if q6==3 & 	grade==3 & test_type==1
replace q7_score=1 	if q7==3 & 	grade==3 & test_type==1
replace q8_score=1 	if q8==2 & 	grade==3 & test_type==1
replace q9_score=1 	if q9==4 & 	grade==3 & test_type==1
replace q10_score=1 if q10==3 & grade==3 & test_type==1

*POR4

replace q1_score=1 	if q1==2 & 	grade==4 & test_type==1
replace q2_score=1 	if q2==1 & 	grade==4 & test_type==1
replace q3_score=1 	if q3==3 & 	grade==4 & test_type==1
replace q4_score=1 	if q4==3 & 	grade==4 & test_type==1
replace q5_score=1 	if q5==2 & 	grade==4 & test_type==1
replace q6_score=1 	if q6==2 & 	grade==4 & test_type==1
replace q7_score=1 	if q7==3 & 	grade==4 & test_type==1
replace q8_score=1 	if q8==4 & 	grade==4 & test_type==1
replace q9_score=1 	if q9==1 & 	grade==4 & test_type==1
replace q10_score=1 if q10==2 & grade==4 & test_type==1

*POR5

replace q1_score=1 	if q1==3 & 	grade==5 & test_type==1
replace q2_score=1 	if q2==2 & 	grade==5 & test_type==1
replace q3_score=1 	if q3==4 & 	grade==5 & test_type==1
replace q4_score=1 	if q4==4 & 	grade==5 & test_type==1
replace q5_score=1 	if q5==3 & 	grade==5 & test_type==1
replace q6_score=1 	if q6==3 & 	grade==5 & test_type==1
replace q7_score=1 	if q7==4 & 	grade==5 & test_type==1
replace q8_score=1 	if q8==4 & 	grade==5 & test_type==1
replace q9_score=1 	if q9==2 & 	grade==5 & test_type==1
replace q10_score=1 if q10==1 & grade==5 & test_type==1

*MAT3

replace q1_score=1 	if q1==3 & 	grade==3 & test_type==2
replace q2_score=1 	if q2==3 & 	grade==3 & test_type==2
replace q3_score=1 	if q3==3 & 	grade==3 & test_type==2
replace q4_score=1 	if q4==2 & 	grade==3 & test_type==2
replace q5_score=1 	if q5==1 & 	grade==3 & test_type==2
replace q6_score=1 	if q6==4 & 	grade==3 & test_type==2
replace q7_score=1 	if q7==1 & 	grade==3 & test_type==2
replace q8_score=1 	if q8==2 & 	grade==3 & test_type==2
replace q9_score=1 	if q9==2 & 	grade==3 & test_type==2
replace q10_score=1 if q10==1 & grade==3 & test_type==2

*MAT4

replace q1_score=1 	if q1==3 & 	grade==4 & test_type==2
replace q2_score=1 	if q2==2 & 	grade==4 & test_type==2
replace q3_score=1 	if q3==4 & 	grade==4 & test_type==2
replace q4_score=1 	if q4==2 & 	grade==4 & test_type==2
replace q5_score=1 	if q5==3 & 	grade==4 & test_type==2
replace q6_score=1 	if q6==3 & 	grade==4 & test_type==2
replace q7_score=1 	if q7==2 & 	grade==4 & test_type==2
replace q8_score=1 	if q8==4 & 	grade==4 & test_type==2
replace q9_score=1 	if q9==1 & 	grade==4 & test_type==2
replace q10_score=1 if q10==2 & grade==4 & test_type==2

*MAT5

replace q1_score=1 	if q1==2 & 	grade==5 & test_type==2
replace q2_score=1 	if q2==4 & 	grade==5 & test_type==2
replace q3_score=1 	if q3==1 & 	grade==5 & test_type==2
replace q4_score=1 	if q4==2 & 	grade==5 & test_type==2
replace q5_score=1 	if q5==4 & 	grade==5 & test_type==2
replace q6_score=1 	if q6==1 & 	grade==5 & test_type==2
replace q7_score=1 	if q7==3 & 	grade==5 & test_type==2
replace q8_score=1 	if q8==2 & 	grade==5 & test_type==2
replace q9_score=1 	if q9==2 & 	grade==5 & test_type==2
replace q10_score=1 if q10==3 & grade==5 & test_type==2

egen score = rowtotal(q1_score q2_score q3_score q4_score q5_score q6_score q7_score q8_score q9_score q10_score)

sort test_type grade
by test_type grade: sum score

lab var score "Total student test score"

order school_id test_type grade class student_id

egen nonfill = anycount(q1 q2 q3 q4 q5 q6 q7 q8 q9 q10), values(0)

lab var nonfill "Number of unanswered questions"

***********

gen municipality = .
replace municipality = 1 if inlist(school_id,1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,22,24,25,26,27,28,29,30,31,32,33,34,35,37,40,45,61,86,110)
replace municipality = 2 if inlist(school_id,23,43,44,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,62,63,64,65,66,67,68,69,70,71,72,73,75,76,77,78,79,80,81)
replace municipality = 3 if inlist(school_id,11,36,38,39,41,42,74,82,83,84,85,87,88,89,90,91,92,93,94,95,96,97,98,99,101,102,103,104,105)
replace municipality = 4 if inlist(school_id,118,119,120,121,122,123,124,125)
replace municipality = 5 if inlist(school_id,100,112,113,114,115,116,117)
replace municipality = 6 if inlist(school_id,109,111)
replace municipality = 7 if inlist(school_id,106,107)
replace municipality = 8 if inlist(school_id,126)
replace municipality = 9 if inlist(school_id,108)


gen treatment = .
replace treatment = 0 if inlist(school_id,3,4,7,10,16,18,20,23,26,29,33,39,42,53,55,62,63,64,65,78,81,84,87,88,89,92,98,108,109,116,117,121)
replace treatment = 1 if inlist(school_id,15,19,22,27,32,36,37,40,49,51,56,57,58,61,66,68,72,76,79,82,83,86,91,95,100,102,103,106,110,114,125,126)
replace treatment = 2 if inlist(school_id,1,5,6,11,12,14,21,24,25,31,41,48,50,52,59,70,73,77,80,90,93,94,101,105,107,111,112,113,119,120,124)
replace treatment = 3 if inlist(school_id,2,8,9,13,17,28,30,34,35,38,43,44,45,46,47,54,60,67,69,71,74,75,85,96,97,99,104,115,118,122,123)

order municipality treatment, after(school_id)

lab var municipality "Municipality"
lab var treatment "Treatment group"

#delimit ;
;
label define treatment
0	"Control group"
1	"Door-to-door"
2	"Meetings"
3	"Both treatments"
;
label define municipality
1	"Sumbe"
2	"Porto Amboim"
3	"Gabela"
4	"Conda"
5	"Ebo"
6	"Quilenda"
7	"Seles"
8	"Cassongue"
9	"Quibala"
;
#delimit cr

lab val municipality municipality
lab val treatment treatment

sort _all

gen test_id = _n

*************

save "$Endline_dtRaw/_intermediate files/tests.dta", replace
